package ningbo.api;

import java.lang.reflect.Method;

/**
 * @Author 宁铂
 * @Date 2021/7/17 13:40
 * @Version 1.0
 */
public class TestString3 {
    /** 本类用于字符串的拼接 */
    public static void main(String[] args) {
        method1();
        method2();
    }

    /** 高效字符串拼接 */
    private static void method2() {
        String str1 = "abcdefghigklmnopqrstuvwsyz";

        StringBuilder stringBuilder = new StringBuilder();
        StringBuffer stringBuffer = new StringBuffer();

        String result = "";
        long t1 = System.currentTimeMillis();

        for (int i = 1; i <= 10000; i++){
            stringBuffer.append(str1);
        }

        long t2 = System.currentTimeMillis();

        System.out.println("拼接的结果： "+ stringBuffer);
        System.out.println("花费时间："+(t2-t1));
    }

    /** 普通字符串拼接 */
    private static void method1() {
        /** 需求：拼接26个小写字母10000次 */
        String str = "abcdefghigklmnopqrstuvwsyz";

        String result = "";
        long t1 = System.currentTimeMillis();

        for (int i = 1; i <= 10000; i++){
            result = result + str;
        }

        long t2 = System.currentTimeMillis();

        System.out.println("拼接的结果： "+ result);
        System.out.println("花费时间："+(t2-t1));
    }
}
